package com.ybn.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ybn.entity.VoteItem;
import com.ybn.entity.VoteSubject;
import com.ybn.service.VoteSubjecService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/vote_subject")
public class VoteSubjectController {
    @Resource
    VoteSubjecService voteSubjecService;

    @RequestMapping("/index")
    @ResponseBody
    @CrossOrigin
    public String findSubjecByPage(
            @RequestParam(name = "pageindex",required = false,defaultValue = "1")int pageindex,
            @RequestParam(name = "pagesize",required = false,defaultValue = "4")int pagesize,
            HttpServletRequest request
            ){
        Page<VoteSubject> page = new Page<>(pageindex,pagesize);
        IPage<VoteSubject> pages = voteSubjecService.findAll(page);
        Map map = new HashMap();
        map.put("subjectPages",pages);
        map.put("pagesize",pagesize);
        for (VoteSubject sub:pages.getRecords()){
            System.out.println(sub.getVstitle());
            for (VoteItem item:sub.getItems()){
                System.out.println(item);
            }
        }
        request.setAttribute("subjectPages",pages);
        request.setAttribute("pagesize",pagesize);
        return "index";
    }

//    @RequestMapping("/findSubjectByVsId")
//    public String findSubjectByVsId(int vsid,HttpServletRequest request){
//        VoteSubject voteSubject = voteSubjecService.findSubjectByVsId(vsid);
//        request.setAttribute("sub",voteSubject);
//    }
}
